package com.linkoog.devtools.dubbo.parser.xml;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.linkoog.devtools.dubbo.parser.DubboService;
import com.linkoog.devtools.dubbo.parser.DubboServiceParser;
import com.linkoog.devtools.dubbo.parser.annotation.AnnotationDubboServiceParser;
import com.linkoog.devtools.dubbo.parser.xml.index.XmlDubboFileIndex;

public class XmlDubboServiceParser implements DubboServiceParser {

    public static AnnotationDubboServiceParser getInstance(){
        return EXTENSION_POINT_NAME.findExtension(AnnotationDubboServiceParser.class);
    }

    @Override
    public DubboService parse(PsiClass psiClass){
        if (psiClass == null) return null;

        Project project = psiClass.getProject();
        DubboService dubboService = XmlDubboFileIndex.getServiceVoByName(project, psiClass.getQualifiedName());
        if (dubboService != null){
            return dubboService;
        }

        PsiClass[] superPsiClassArray =  psiClass.getSupers();
        for (PsiClass superPsiClass : superPsiClassArray) {
            dubboService = XmlDubboFileIndex.getServiceVoByName(project, superPsiClass.getQualifiedName());
            if (dubboService != null){
                return dubboService;
            }
        }
        return  null;
    }

}
